package com.cecsmsserve.service.impl;

import com.cecsmsserve.entity.Activity;
import com.cecsmsserve.entity.User;
import com.cecsmsserve.entity.vo.SignedUserList;
import com.cecsmsserve.mapper.ActivityMapper;
import com.cecsmsserve.mapper.UserActivityMapper;
import com.cecsmsserve.mapper.UserMapper;
import com.cecsmsserve.service.IActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cecsmsserve.util.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王郝浠
 * @since 2024-04-17
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    UserActivityMapper userActivityMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public CommonResult selectAll() {
        List<Activity> list=activityMapper.selectAll();
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectAllByPage(int current, int size) {
        int start=(current-1)*size;
        List<Activity> list=activityMapper.selectAllByPage(start,size);
        List<Activity> getTotal=activityMapper.selectList(null);
        int total=getTotal.size();
        CommonResult result=new CommonResult(String.valueOf(total),list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectNotBegin() {
        List<Activity> list=activityMapper.selectNotBegin();
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByName(String name) {
        List<Activity> list=activityMapper.selectByName(name);
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByNameByPage(String name, int current, int size) {
        int start=(current-1)*size;
        List<Activity> list=activityMapper.selectByNameByPage(name,start,size);
        List<Activity> getTotal=activityMapper.selectByName(name);
        int total=getTotal.size();
        CommonResult result=new CommonResult(String.valueOf(total),list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByState(int state) {
        List<Activity> list=activityMapper.selectByState(state);
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByType(int type) {
        List<Activity> list=activityMapper.selectByType(type);
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectById(int id) {
        Activity activity=activityMapper.selectById(id);
        CommonResult result=new CommonResult(activity);
        if(activity==null){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult insert(Activity activity) {
        activity.setId(null);
        int i=activityMapper.insert(activity);
        CommonResult result=new CommonResult(activity);
        if(i<0){
            result.setNotInserted();
        }
        return result;
    }

    @Override
    public CommonResult update(Activity activity) {
        int i=activityMapper.updateById(activity);
        CommonResult result=new CommonResult(i);
        if(i<=0){
            result.setNotUpdate();
        }
        return result;
    }

    @Override
    public CommonResult countSignedUpNum() {
        List<HashMap<String,Object>> list=activityMapper.countSignedUpNum();
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult countActivitySort() {
        List<HashMap<String,Object>> list=activityMapper.countActivitySort();
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    /**
     * 从第0分钟开始每30分钟执行一次，修改活动状态
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void updateActivityStatus(){
        List<Activity> list=activityMapper.selectByState(1);
        LocalDateTime now=LocalDateTime.now();

        for(Activity activity:list){
            LocalDateTime startTime=LocalDateTime.of(activity.getActivityDate(),activity.getStartTime());
            LocalDateTime endTime=LocalDateTime.of(activity.getActivityDate(),activity.getEndTime());
            if(!startTime.isAfter(now)){
                //活动已经开始
                if(endTime.isAfter(now)) {
                    activity.setState(2); //状态为“报名截止”
                }
                if (!endTime.isAfter(now)){
                    activity.setState(3); //状态为“已结束”
                    int aId=activity.getId();
                    List list1=userActivityMapper.selectUserByaId(aId);
                    if(list1.size()>0){
                        for(int i=0;i<list1.size();i++){
                            int id= (int) list1.get(i);
                            User user=userMapper.selectById(id);
                            user.setPoint(user.getPoint()+activity.getActivityPoint());
                            userMapper.updateById(user);
                        }
                    }
                }
                activityMapper.updateById(activity);
                System.out.println("状态已更新："+activity.getState());
            }
        }
    }
}
